package com.motong.gongdan.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.motong.gongdan.entity.CustomField;
import com.motong.gongdan.entity.MobileCardConf;
import com.motong.gongdan.enums.ObjectType;
import com.motong.gongdan.mapper.MobileCardConfMapper;
import com.motong.gongdan.util.ColumnUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class MobileCardConfService extends ServiceImpl<MobileCardConfMapper, MobileCardConf> {

    @Resource
    private CustomFieldService customFieldService;

    /**
     * 获取去制定模块的自定义卡片设置(自定义字段放置自定义字段id)
     *
     * @param columnModule 所属模块
     */
    public List<MobileCardConf> getColumnModuleList(Integer columnModule) {
        List<MobileCardConf> data = this.list(new LambdaQueryWrapper<MobileCardConf>().eq(MobileCardConf::getBelongModule, columnModule).orderByAsc(MobileCardConf::getSort));
        ColumnUtils workOrderCustomField = new ColumnUtils(customFieldService, ObjectType.workOrder.getCode());
        ColumnUtils processesCustomField = new ColumnUtils(customFieldService, ObjectType.processes.getCode());
        data.forEach(i -> {
            if ("virtualColumn".equals(i.getEnName())) {
                switch (i.getEnName()) {
                    case "产品信息":
                        i.setEnName("productInfo");
                        break;
                    case "工单进度":
                        i.setEnName("workOrderInfo");
                        break;
                    default:
                        break;
                }
            }
            if (Objects.isNull(i.getEnName())) {
                CustomField customField = (ObjectType.workOrder.getCode().equals(i.getColumnModule()) ? workOrderCustomField.getCustomFieldByCn(i.getCnName()) : processesCustomField.getCustomFieldByCn(i.getCnName()));
                i.setCustomColumnType(customField.getFieldType());
                i.setCustomFieldId(customField.getId());
                if (Objects.nonNull(customField.getId())) {
                    i.setEnName(customField.getId().toString());
                }

            }
            if ("workOrder".equals(i.getColumnModule()) && "code".equals(i.getEnName())) {
                i.setEnName("workOrderCode");
            }
            if ("workOrder".equals(i.getColumnModule()) && "unit".equals(i.getEnName())) {
                i.setEnName("productUnit");
            }

        });
        return data;
    }
}
